<template>
    <div class="address-list">
        <van-nav-bar title="设置地址" left-text="返回" left-arrow right-text="设为默认" fixed @click-left="back"
            @click-right="confirm" />
        <van-address-list v-model="chosenAddress.id" :list="list" 
        default-tag-text="默认" 
        @add="onAdd" 
        @edit="onEdit" 
        @select="select"/>
    </div>
</template>
<script>
    import '../assets/scss/AddressList.scss';
    export default {
        name: 'AddressList',
        data() {
            return {
                chosenAddress: '',
                // 完全数据
                adrList: [],
                // 显示数据
                list: [
                    // {
                    //     id: '2',
                    //     name: '李四',
                    //     tel: '1310000000',
                    //     address: '浙江省杭州市拱墅区莫干山路 50 号',
                    // },
                ],
            };
        },
        created() {
            this.getAddressList();
        },
        methods: {
            //返回上一级
            back() {
                this.$router.go(-1);
            },
            // 添加按钮
            onAdd() {
                this.$router.push({
                    name: 'AddressEdit'
                })
            },
            select(item, index) {
                console.log(index, '==>', item);
                this.chosenAddress = item;
            },
            confirm() {
                console.log(this.chosenAddress);
                // console.log('content ==> ', content);

                let content = this.chosenAddress;
                let url = '/editAddress';
                let data = {
                    appkey: this.appkey,
                    tokenString: localStorage.getItem('__tk'),

                    name: content.name,
                    tel: content.tel,
                    province: content.province,
                    city: content.city,
                    county: content.county,
                    addressDetail: content.addressDetail,
                    areaCode: content.areaCode,
                    postalCode: content.postalCode,
                    isDefault: 1,
                    aid:content.aid
                }
       
                this.axios({
                        //请求类型
                        method: "POST",
                        url,
                        data
                    })
                    .then((result) => {
                        this.$toast.clear();
                        this.$toast.success(result.data.msg);
                        console.log("setDefaut result ==> ", result);

                        // 更新列表
                        this.getAddressList();
                    })
                    .catch((err) => {
                        this.$toast.clear();
                        console.log("err ==> ", err);
                    });
            },
            // 跳转到编辑页面
            onEdit(item, index) {
                console.log(item);
                this.$router.push({
                    name: 'AddressEdit',
                    params: {
                        aid: item.aid
                    }
                })
            },
            // 获取列表信息
            getAddressList() {
                this.$toast.loading({
                    message: '加载中...',
                    forbidClick: true,
                });
                this.axios({
                        method: 'GET',
                        url: '/findAddress',
                        params: {
                            appkey: this.appkey,
                            tokenString: localStorage.getItem('__tk')
                        }
                    })
                    .then(result => {
                        console.log('请求地址list=>', result);


                        if (result.data.code === 20000) {
                            this.$toast.clear();
                            this.adrList = result.data.result;
                            let addree = [];
                            result.data.result.map(item => {
                                addree.push({
                                    ...item,
                                    address: `${item.city} ${item.county} ${item.addressDetail}`
                                })
                                // 确认默认id
                                if (item.isDefault)
                                    this.chosenAddress = item;
                            })
                            this.list = addree;
                        }
                    })
                    .catch(err => {
                        console.log('err=>', err);
                    })
            }
        },
    };
</script>